package timeline;

import java.io.*;
import java.net.*;
import java.util.*;

public class AnalysisTimelines {
	String msg_file ="./output/TimeLine_msg.txt";
	String user_file ="./output/TimeLine_user.txt";
	
	Util util;

	AnalysisTimelines(){
		util = Util.getInstance(true);
    
    }
	
	void CountUsers() throws Exception
	{
		FileReader user_fr = new FileReader(user_file);
        BufferedReader user_br = new BufferedReader(user_fr);
        FileReader msg_fr = new FileReader(msg_file);
        BufferedReader msg_br = new BufferedReader(msg_fr);
        String user_line;
        String msg_line;
        
        Map<String, String> recentMsgMap = new HashMap<String, String>();
        
        Map<String, Integer> userMap = new HashMap<String, Integer>();
        Map<String, Integer> msgMap = new HashMap<String, Integer>();
        Map<String, Integer> urlMap = new HashMap<String, Integer>();
        Map<String, Integer> domainMap = new HashMap<String, Integer>();
        int sum =0;
        
       while(true)
       {
    	   user_line = user_br.readLine();
           msg_line = msg_br.readLine();
           sum++;
           if ((user_line == null)||(msg_line == null)) break;
           if (user_line.length()==0) continue;
    	   // count the number of users
            if (! userMap.containsKey(user_line)){
            	userMap.put(user_line, new Integer(1));            	
            	recentMsgMap.put(user_line, msg_line);
            }
            else
            {
            	if (msg_line != recentMsgMap.get(user_line))
            	{
            		Integer num= userMap.get (user_line);
                	num ++;
                	//System.out.println(line+": "+num);
                	userMap.put(user_line, num);
                	recentMsgMap.put(user_line, msg_line);            		
            	}
            	else
            	{
            		System.out.println("Dpulicate Line. user: "+user_line+" msg: " +msg_line);
            		continue;
            	}
            	
             }
            
            //count the number of messages
            if (! msgMap.containsKey(msg_line)){
            	msgMap.put(msg_line, new Integer(1));
            }
            else
            {
            	Integer num= msgMap.get (msg_line);
            	num ++;
            	msgMap.put(msg_line, num);
            }
            
            
            ArrayList<String> urlList = util.getUrl(msg_line);
            
            Iterator<String> iter = urlList.iterator();
            
            while (iter.hasNext())
            {
            	String url = iter.next();
            	
                if (! urlMap.containsKey(url)){
                	urlMap.put(url, new Integer(1));
                }
                else
                {
                	Integer num= urlMap.get (url);
                	num ++;
                	msgMap.put(url, num);
                }
                
                String domain = util.getDomain(url);
                if (! domainMap.containsKey(domain)){
                	domainMap.put(domain, new Integer(1));
                }
                else
                {
                	Integer num= domainMap.get (domain);
                	num ++;
                	domainMap.put(domain, num);
                }
                
                //String pagefile = "./output/webpage/"+user_line+"_"+url;
               // String pagefile = "./output/webpage/"+user_line+"_";
               // util.wget(pagefile, url);
       
            }
                                    
        }
       
       System.out.println("Total Number: "+sum);
       PrintStream user_outfile = new PrintStream (new FileOutputStream("./output/Count_user.xls"));
       PrintStream msg_outfile = new PrintStream (new FileOutputStream("./output/Count_msg.xls"));
       PrintStream url_outfile = new PrintStream (new FileOutputStream("./output/Count_url.xls"));
       PrintStream domain_outfile = new PrintStream (new FileOutputStream("./output/Count_domain.xls"));
       user_outfile.println("Name\tCount");
       msg_outfile.println("Message\tCount");
       url_outfile.println("URL\tCount");
       domain_outfile.println("Domain\tCount");
       
       Iterator<Map.Entry<String, Integer>> it = userMap.entrySet().iterator();
       while (it.hasNext()) 
       {
           Map.Entry pairs = (Map.Entry)it.next();
           user_outfile.println(pairs.getKey() + "\t" + pairs.getValue());
       }
       
       Iterator<Map.Entry<String, Integer>> it2 = msgMap.entrySet().iterator();
       while (it2.hasNext()) 
       {
           Map.Entry<String, Integer> pairs = it2.next();
           msg_outfile.println(pairs.getKey() + "\t" + pairs.getValue());
       }
       
       Iterator<Map.Entry<String, Integer>> it3 = urlMap.entrySet().iterator();
       while (it3.hasNext()) 
       {
           Map.Entry<String, Integer> pairs = it3.next();
           url_outfile.println(pairs.getKey() + "\t" + pairs.getValue());
       }
       
       Iterator<Map.Entry<String, Integer>> it4 = domainMap.entrySet().iterator();
       while (it4.hasNext()) 
       {
           Map.Entry<String, Integer> pairs = it4.next();
           domain_outfile.println(pairs.getKey() + "\t" + pairs.getValue());
       }
       
       user_outfile.close();
       msg_outfile.close();
       url_outfile.close();
       domain_outfile.close();
       user_fr.close();
       msg_fr.close();
      
	}
	

    public static void main(String[] args) {
        
        AnalysisTimelines myobj = new AnalysisTimelines();
        System.out.println("Start....");
      
        try{
        	myobj.CountUsers();
        }
        catch(Exception exception) {exception.printStackTrace();}
    }
}
